import type { RouteRecordRaw } from 'vue-router'
let firstMenu: any = null
export function mapMenusToRoutes(userMenus: any[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []
  // 1.选取加载默认所有的routes
  const allRoutes: RouteRecordRaw[] = []
  /*
       todo
       require.context(path,useSubdirectories,regExp)
       path:表示检索的目录
       useSubdirectories:是否递归检索子目录
       regExp:正则表达式 检索匹配的文件
  */
  const routeFiles = require.context('../router/main', true, /\.ts/)
  const filePath = routeFiles.keys().forEach((key) => {
    const route = require('../router/main' + key.split('.')[1]) //拼接路径加载router文件夹下面的文件内容
    allRoutes.push(route.default)
  })

  // 2. 根据菜单获取需要添加的routes
  // userMenus
  // 要判断type===1？取children如果children中的type===1继续取children
  // 如果type ===2 ？再去遍历里面的数据
  const _recurseGetRoute = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 2) {
        // allRoutes里面装的是映射的全部的路由文件 如果type == 2就代表要映射路由
        const route = allRoutes.find((route) => route.path === menu.url)
        if (route) routes.push(route)
        if (!firstMenu) firstMenu = menu //记录第一个菜单地址
      } else {
        _recurseGetRoute(menu.children)
      }
    }
  }
  _recurseGetRoute(userMenus)
  return routes
}

// todo coderwhy 递归实现拿到此路由页面对应菜单的id
export function pathMapToMenu(userMenus: any[], currentPath: string, breadcrumbs?: any[]): any {
  for (const menu of userMenus) {
    if (menu.type == 1) {
      const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
      if (findMenu) {
        breadcrumbs?.push({ name: menu.name, path: menu.url })
        breadcrumbs?.push({ name: findMenu.name, path: findMenu.url })
        return findMenu
      }
    } else if (menu.type == 2 && menu.url == currentPath) {
      return menu
    }
  }
}
// todo wyc
export function menus(userMenus: any[], currentPath: any) {
  for (const menu of userMenus) {
    // 根据type判断点击该点击的还是不该点击的
    if (menu.type == 1) {
      const currentMenu = menu.children.filter((item: any) => item.url == currentPath)
      if (currentMenu.length) {
        return currentMenu[0]
      }
    } else if (menu.type == 2) {
      return menu
    }
  }
}
// 映射面包屑路径方法
export function pathMapBreadcrumbs(userMenus: any, currentPath: any) {
  const breadcrumbs: any[] = []

  pathMapToMenu(userMenus, currentPath, breadcrumbs)
  return breadcrumbs
}
//按钮权限控制
export function mapMenusToPermissions(userMenus: any[]) {
  const permission: string[] = []
  const _recurseGetPermission = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 1 || menu.type === 2) {
        _recurseGetPermission(menu.children ?? [])
      } else if (menu.type === 3) {
        // 只有第三层有permission
        permission.push(menu.permission)
      }
    }
  }
  _recurseGetPermission(userMenus)
  return permission
}
// 返回菜单的页子节点
export function getMenuLeafKeys(menusList: any[]) {
  const leftKeys: number[] = []

  const _recurseGetLeaf = (menuList: any[]) => {
    for (const menu of menuList) {
      if (menu.children) {
        _recurseGetLeaf(menu.children)
      } else {
        leftKeys.push(menu.id)
      }
    }
  }
  _recurseGetLeaf(menusList)
  return leftKeys
}
export { firstMenu }
